﻿$(function() {
    JSUtility.param.Options["InternalType"] = "2";
    JSUtility.url = "../../Handler/AjaxHandle.ashx";
    JSUtility.param.ClassName = "OA.Bll.Project.ContractInfoPresenter";
    autoResize({ dataGrid: '#Grid', callback: Initlist });

    JSUtility.window_edit = $('#Add').window({
        iconCls: 'icon-add',
        zIndex: 1000,
        draggable: true,
        resizable: false,
        shadow: true,
        inline: true,
        modal: true,
        title: '添加合同信息',
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closable: true,
        width: 610,
        //height: 400,
        closed: true,
        onOpen: function() {
            $('#Add').parent().css("left", "10%");
            $('#Add').parent().next().css("left", "10%");
            $('#Add').parent().next().css("top", "10%");
            $('#Add').parent().css("top", "10%");
        }
    });

    JSUtility.window_edit_title = "合同信息";
    JSUtility.window_edit_form = $('#Add').find("form");
    ComboboxInitial();
    $('#Add').show();
});
function ComboboxInitial() {
    //    $('#ContractsStateID').combobox({
    //        url: '',
    //        textField: 'Name',
    //        valueField: 'Id'
    //    });
    //    $('#ISOutsourcing').combobox({ editable: false });
    if (!self.parent.projectInfoManager) {
        JSHelper.combotreeProject('ProjectId', "../../Handler/AjaxProjectTreeHandle.ashx?internaltype=2", 135);
    }
}


function Initlist(size) {
    if (self.parent.projectInfoManager) {
        JSUtility.param.Options["ProjectId"] = self.parent.parent.project_id;
        JSUtility.replenish = function() {
            $("#ProjectId").val(self.parent.parent.project_id);
        };
        $("#ProjectId").attr("readonly", "readonly");
    }
    JSUtility.param.Method = "Search";
    JSUtility.grid = $('#Grid').datagrid({
        tilte: '合同基本信息',
        iconCls: 'icon-contract',
        width: size.width,
        height: size.height,
        pageSize: Math.round(size.height / 25),
        pageList: [Math.round(size.height / 25)],
        nowrap: true,
        striped: true,
        url: JSUtility.url,
        queryParams: { "json": $.toJSON(JSUtility.param) },
        sortName: 'Id',
        sortOrder: 'desc',
        idField: 'Id',
        frozenColumns: [[{ title: '编号', field: 'Id', width: 80, sortable: true}]],
        columns: [[
                            { title: '外部合同编号', field: 'OuterNum', width: 120 },
                            { title: '内部合同编号', field: 'InnerNum', width: 80 },
                            { title: '合同名称', field: 'ContractName', width: 100 },
                            { title: '项目编号', field: 'ProjectId', width: 80 },
                            { title: '项目名称', field: 'ProjectName', width: 80 },
        // { title: '合同状态', field: 'ContractsStateID.Name', width: 100 },
                            {title: '开始日期', field: 'StartTime', width: 100 },
                            { title: '终止日期', field: 'FinishedTime', width: 100 },
                            { title: '项目预算合计', field: 'opt', width: 100,
                                formatter: function(value, rowData, rowIndex) {
                                    var fundsAmount = parseFloat(rowData.RailwayFunds) + parseFloat(rowData.CountryFunds) + parseFloat(rowData.LocaLGovernmentFunds) +
                                        parseFloat(rowData.GroupFunds) + parseFloat(rowData.CompanyFunds) + parseFloat(rowData.BankFunds) + parseFloat(rowData.OtherFunds);
                                    return '<span style="color:red">' + fundsAmount + '</span>';
                                }
                            },
                            { title: '铁道部科研计划拨款', field: 'RailwayFunds', width: 100 },
                            { title: '国家其他拨款', field: 'CountryFunds', width: 100 },
                            { title: '地方政府拨款', field: 'LocaLGovernmentFunds', width: 100 },
                            { title: '集团公司拨款', field: 'GroupFunds', width: 100 },
                            { title: '单位自筹款', field: 'CompanyFunds', width: 100 },
                            { title: '银行贷款', field: 'BankFunds', width: 100 },
                            { title: '其它来源', field: 'OtherFunds', width: 100 }


                        ]],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [
                    {
                        text: '添加',
                        iconCls: 'icon-add',
                        handler: JSUtility.add
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function() {
                            JSUtility.update();
                            if (JSUtility.row == null) return;
                            var rowData = JSUtility.row;
                            var fundsAmount = parseFloat(rowData.RailwayFunds) + parseFloat(rowData.CountryFunds) + parseFloat(rowData.LocaLGovernmentFunds) +
                                        parseFloat(rowData.GroupFunds) + parseFloat(rowData.CompanyFunds) + parseFloat(rowData.BankFunds) + parseFloat(rowData.OtherFunds);
                            $("#FundsAmount").val(fundsAmount);
                            JSHelper.conbotreeUpdateProject("ProjectId");
                        }
                    }, '-',
                    {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: JSUtility.del
                    }, '-',
                    {
                        text: '查询',
                        iconCls: 'icon-search',
                        handler: JSUtility.search
                    }, '-',
                    {
                        text: '详细信息',
                        iconCls: 'icon-edit',
                        handler: contractsInfoOpen
                    }
                  ]
    });
}
function contractsInfoOpen() {
    var row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    if (!self.parent.projectInfoManager) {
        self.parent.contracts_id = row.OuterNum;
        self.parent.contracts_Name = row.ContractName;
        self.parent.addTab("合同详细信息", "Project/ScientificProject/ContractsInfoManager.htm", "icon-edit");
    } else {
        self.parent.parent.contracts_id = row.OuterNum;
        self.parent.parent.contracts_Name = row.ContractName;
        self.parent.projectInfoManager.addTab("合同详细信息", "ContractsInfoManager.htm", "icon-edit");
    }
}